<template>
	<view class="fui-wrap" :style="`padding-bottom:${safebottom}rpx;`">
		<fui-banner-arc background="linear-gradient(to bottom, #13a1ff, #9cd6f9)">
			<view class="userinfo" v-if="userinfo">
				<fui-avatar width="120" height="120" :src="userinfo.avatar"></fui-avatar>
				<view class="user">
					<fui-text :text="userinfo.nickname" color="#fff" block :size="28"></fui-text>
					<fui-text :text="userinfo.mobile" color="#fff" block :size="28"></fui-text>
				</view>
			</view>
		</fui-banner-arc>
		<fui-card :margin="['0','32rpx','30rpx','32rpx']" @click="scancode">
			<fui-list :topBorder="false">
				<fui-list-cell :bottomBorder="false" :padding="['32rpx','24rpx']" arrow>
					<image :src="`${baseUrl}assets/img/miniapp/scan.png`" style="width: 40rpx;height: 40rpx;" mode="widthFix"></image>
					<fui-text style="margin-left: 10rpx;" text="识别二维码" color="#000" block :size="28"></fui-text>
				</fui-list-cell>
			</fui-list>
		</fui-card>
		<fui-card :margin="['0','32rpx','30rpx','32rpx']" @click="$navigateTo('parking/log')">
			<fui-list :topBorder="false">
				<fui-list-cell :bottomBorder="false" :padding="['32rpx','24rpx']" arrow>
					<image :src="`${baseUrl}assets/img/miniapp/log.png`" style="width: 40rpx;height: 40rpx;" mode="widthFix"></image>
					<fui-text style="margin-left: 10rpx;" text="停车场日志" color="#000" block :size="28"></fui-text>
				</fui-list-cell>
			</fui-list>
		</fui-card>
		<fui-card :margin="['0','32rpx','30rpx','32rpx']">
			<fui-list :topBorder="false">
				<fui-button open-type="contact" type="link">
				<fui-list-cell style="width: 100%;" :bottomBorder="false" :padding="['32rpx','24rpx']" arrow>
					<image :src="`${baseUrl}assets/img/miniapp/kefu.png`" style="width: 40rpx;height: 40rpx;" mode="widthFix"></image>
					<fui-text style="margin-left: 10rpx;" text="我的客服" color="#000" block :size="28"></fui-text>
				</fui-list-cell>
				</fui-button>
			</fui-list>
		</fui-card>
		<fui-card :margin="['0','32rpx','30rpx','32rpx']" @click="$navigateTo('parking/password')">
			<fui-list :topBorder="false">
				<fui-list-cell :bottomBorder="false" :padding="['32rpx','24rpx']" arrow>
					<image :src="`${baseUrl}assets/img/miniapp/password.png`" style="width: 40rpx;height: 40rpx;" mode="widthFix"></image>
					<fui-text style="margin-left: 10rpx;" text="修改密码" color="#000" block :size="28"></fui-text>
				</fui-list-cell>
			</fui-list>
		</fui-card>
		<fui-card :margin="['0','32rpx','30rpx','32rpx']">
			<fui-list :topBorder="false">
				<fui-list-cell @click="$navigateTo('parking/list')" :bottomBorder="false" :padding="['32rpx','24rpx']" arrow>
					<image :src="`${baseUrl}assets/img/miniapp/qiehuan.png`" style="width: 40rpx;height: 40rpx;" mode="widthFix"></image>
					<fui-text style="margin-left: 10rpx;" text="切换账户" color="#000" block :size="28"></fui-text>
				</fui-list-cell>
			</fui-list>
		</fui-card>
		<fui-card :margin="['0','32rpx','30rpx','32rpx']"  @click="logout">
			<fui-list :topBorder="false">
				<fui-list-cell :bottomBorder="false" :padding="['32rpx','24rpx']" arrow>
					<image :src="`${baseUrl}assets/img/miniapp/logout.png`" style="width: 40rpx;height: 40rpx;" mode="widthFix"></image>
					<fui-text style="margin-left: 10rpx;" text="退出登录" color="#000" block :size="28"></fui-text>
				</fui-list-cell>
			</fui-list>
		</fui-card>
		<toolbar :current="3"></toolbar>
	</view>
</template>

<script>
	import {methods,baseUrl} from '@/utils/core.js';
	import toolbar from './components/toolbar.vue';
	export default {
		components:{
			toolbar:toolbar
		},
		data() {
			return {
				baseUrl:baseUrl,
				safebottom:0,
				userinfo:''
			}
		},
		onLoad() {
			this.parkingadmin();
			let that=this;
			uni.getSystemInfo({
			  success: function (info) {
				that.safebottom=info.safeAreaInsets.bottom*2+100+30;
			  }
			});
		},
		methods: {
			...methods,
			parkingadmin:function(){
				this.$get('parking/index/parking-admin').then(res=>{
					res.avatar=this.$userinfo('avatar');
					this.userinfo=res;
				});
			},
			logout:function(){
				this.$get('parking/common/logout','',true,true).then(res=>{
					this.$reLaunch('parking/login',1500);
				});
			},
			scancode:function(){
				let that=this;
				uni.authorize({
				    scope: 'scope.camera',
				    success() {
				        uni.scanCode({
							success(res) {
								if (res.result) {
									if(res.result.startsWith('http://weixin.qq.com')){
										uni.showModal({
											title:'无牌车入场码，直接用微信扫码可以出结果',
											showCancel:false
										});
									}else{
										let methods=that.queryURLmethods(res.result);
										let obj = that.queryURLparams(res.result);
										that.showResult(methods,obj);
									}
								} else {
									that.$msg('扫描失败');
								}
							},
							fail() {
								that.$msg('调用相机失败');
							}
						});
				    },
				    fail() {
				        that.$msg('您拒绝了授权');
				    }
				});
			},
			showResult:function(methods,obj){
				if(methods=='exit'){
					this.$get('parking/index/get-qrcode-info',{type:'barrier',serialno:obj.serialno},true,true).then(res=>{
						uni.showModal({
							title:'出口码',
							content:res.title,
							showCancel:false
						});
					});
					return;
				}
				if(methods=='stock'){
					this.$get('parking/index/get-qrcode-info',{type:'stock',uniqid:obj.uniqid},true,true).then(res=>{
						uni.showModal({
							title:'场内付费码',
							showCancel:false
						});
					});
					return;
				}
				if(methods=='monthly'){
					this.$get('parking/index/get-qrcode-info',{type:'monthly',uniqid:obj.uniqid},true,true).then(res=>{
						uni.showModal({
							title:'月租申请与续租码',
							showCancel:false
						});
					});
					return;
				}
				if(methods=='stored'){
					this.$get('parking/index/get-qrcode-info',{type:'stored',uniqid:obj.uniqid},true,true).then(res=>{
						uni.showModal({
							title:'储值卡申请与充值码',
							showCancel:false
						});
					});
					return;
				}
				if(methods=='admin'){
					uni.showModal({
						title:'停车场小程序管理端入口码',
						showCancel:false
					});
					return;
				}
				if(methods=='merchant'){
					uni.showModal({
						title:'停车场小程序商家端入口码',
						showCancel:false
					});
					return;
				}
				if(methods=='miniapp'){
					uni.showModal({
						title:'云起停车小程序用户端入口码',
						showCancel:false
					});
					return;
				}
				uni.showModal({
					title:'二维码不存在',
					showCancel:false
				});
			},
			queryURLmethods:function(url){
				let host=baseUrl.slice(8)
				let len=host.length+7;
				let startIndex=url.indexOf(host+'qrcode')+len;
				let endIndex=url.indexOf('?');
				return url.slice(startIndex,endIndex);
			},
			queryURLparams:function(url) {
			    let obj = {}
			    if (url.indexOf('?') < 0) return obj
			    let arr = url.split('?')
			    url = arr[1]
			    let array = url.split('&')
			    for (let i = 0; i < array.length; i++) {
			        let arr2 = array[i]
			        let arr3 = arr2.split('=')
			        obj[arr3[0]] = arr3[1]
			    }
			    return obj	
			}
		}
	}
</script>

<style lang="scss" scoped>
	.fui-wrap{
		
	}
	.userinfo{
		display: flex;
		align-items: center;
		padding: 30rpx;
		padding-top: 130rpx;
		.user{
			display: flex;
			flex-direction: column;
			height: 80rpx;
			justify-content: space-between;
			margin-left: 30rpx;
		}
	}
</style>